import serial  
from time import sleep  
class SerialPort(object):
    def __init__(self,Name,BaudRate):
        self.ser = serial.Serial(Name, BaudRate, timeout=0.5)   
       
    def Read(self):    
        data=None 
        while True:    
            #count = self.ser.inWaiting() #获取接收缓存区的字节数
            data = self.ser.readline()    
            if data == None:    
                continue  
            else:  
                break  
            sleep(0.002)
            self.ser.flushInput()
        return data

    def Write(self,data):
        self.ser.write(data)

if __name__ == "__main__":
    sp = SerialPort('COM14',9600)
    sp.Write('dfdfdf'.encode())
    while True:
        d = sp.Read()
        print(bytes.decode(d))
